import pymysql
def main():
    no = int(input('部门编号'))
    name = input('部门名称')
    loc = input('地址:')
    # 1 创建连接
    conn = pymysql.connect(host='39.108.212.133',port = 3306,
                           user = 'root', password = '123456',
                           db='hrs', charset = 'utf8',
                           autocommit=True)
    # print (conn)
    try:
        # 2 获得游标对象
        with conn.cursor() as cursor:
            # 3 执行SQL语句得到结果
            result = cursor.execute(
                'insert into tb_dept values(%s,%s,%s)',(no, name, loc))
            if result == 1:
                print('添加成功')
            #4. 操作成功,执行提交
            conn.commit()
    except pymysql.MySQLError as error:
        print(error)
        # 4操作失败,执行回滚
        conn.rollback()

    finally:
        # 5关闭连接
        conn.close()
if __name__=='__main__':
    main()



